﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Wechat.Util.ParmsConfig.ParmsConvert
{
    public static class ParmsConvertUtil
    {
        public static int? FoInt(this int? intParamater, string sParamater)
        {
            var context = HttpContext.Current;
            int outParamater = 0;
            if (!String.IsNullOrWhiteSpace(context.Request[sParamater]))
                int.TryParse(context.Request[sParamater], out outParamater);
            return outParamater;
        }

        /// <summary>
        /// 转换成int类型
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="sParamater"></param>
        /// <returns></returns>
        public static int FoInt(this int intParamater, string sParamater)
        {
            var context = HttpContext.Current;
            intParamater = 0;
            if (!String.IsNullOrWhiteSpace(context.Request[sParamater]))
                int.TryParse(context.Request[sParamater], out intParamater);
            return intParamater;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="sParamater"></param>
        /// <returns></returns>
        public static string FoString(this object strParamater, string sParamater)
        {
            var context = HttpContext.Current;
            strParamater = string.Empty;
            if (!String.IsNullOrWhiteSpace(context.Request[sParamater]))
                strParamater = context.Request[sParamater];

            return strParamater.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strParamater"></param>
        /// <param name="sParamater"></param>
        /// <param name="IsInt"></param>
        /// <returns></returns>
        public static string FoString(this object strParamater, string sParamater, bool IsInt = false)
        {
            var context = HttpContext.Current;
            strParamater = string.Empty;
            if (!String.IsNullOrWhiteSpace(context.Request[sParamater]))
                strParamater = context.Request[sParamater];
            else if (IsInt)
                strParamater = 0;
            return strParamater.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="sParamater"></param>
        /// <returns></returns>
        public static DateTime FoDateTime(this DateTime dateParamater, string sParamater)
        {
            var context = HttpContext.Current;
            dateParamater = DateTime.Now;
            if (!String.IsNullOrWhiteSpace(context.Request[sParamater]))
                dateParamater = Convert.ToDateTime(context.Request[sParamater]);

            return dateParamater;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="decimalParamater"></param>
        /// <param name="sParamater"></param>
        /// <returns></returns>
        public static decimal FoDecimal(this decimal decimalParamater, string sParamater)
        {
            var context = HttpContext.Current;
            decimalParamater = 0;
            if (!String.IsNullOrWhiteSpace(context.Request[sParamater]))
                decimalParamater = Convert.ToDecimal(context.Request[sParamater]);

            return decimalParamater;
        }
    }
}
